package com.qf.air.service.impl;

import com.qf.air.service.AirEsService;
import com.qf.air.util.ElasticsearchUtil;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightField;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.io.IOException;
import java.util.*;

/**
 * @Author songfei
 * @Date 2021/11/4 10:44
 * @Version 1.0
 */
@Service
public class AirEsServiceImpl implements AirEsService {

    @Override
    public Map<String, Object> findByPageAndDistrictId(Integer page, Integer rows, Integer districtId, String monitoringStation, Date startTime, Date endTime) throws IOException {
        SearchRequest request = new SearchRequest();
        request.indices("air");
        SearchSourceBuilder ssb = new SearchSourceBuilder();
        BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
        HighlightBuilder highlightBuilder = new HighlightBuilder();


        if(!StringUtils.isEmpty(districtId)){
            boolQuery.must(QueryBuilders.termQuery("districtId",districtId));
        }
        if(!StringUtils.isEmpty(monitoringStation)){
            boolQuery.must(QueryBuilders.termQuery("monitoringStation",monitoringStation));

            highlightBuilder.fragmentSize(2);
            highlightBuilder.preTags("<span style='color:red;'>");
            highlightBuilder.postTags("</span>");
            highlightBuilder.field("monitoringStation");

        }
        if(!StringUtils.isEmpty(startTime)){
            boolQuery.should(QueryBuilders.rangeQuery("monitorTime").gte(startTime));
        }
        if(!StringUtils.isEmpty(endTime)){
            boolQuery.should(QueryBuilders.rangeQuery("monitorTime").lte(endTime));
        }

        ssb.from((page-1)*rows);
        ssb.size(rows);
        ssb.highlighter(highlightBuilder);
        ssb.query(boolQuery);

        request.source(ssb);
        SearchResponse resp = ElasticsearchUtil.getClient().search(request, RequestOptions.DEFAULT);

        List<Map> airList = new ArrayList<>();
        for (SearchHit hit : resp.getHits().getHits()) {
            HighlightField highlightField = hit.getHighlightFields().get("monitoringStation");
            if(highlightField != null){
                String value = highlightField.getFragments()[0].toString();
                hit.getSourceAsMap().put("monitoringStation",value);
            }
            airList.add(hit.getSourceAsMap());
        }
        Map<String,Object> map = new HashMap<>();
        map.put("count",resp.getHits().getTotalHits().value);

        map.put("list",airList);
        return map;
    }
}
